﻿namespace CKFinder.Connector.CommandHandlers
{
    using CKFinder;
    using CKFinder.Connector;
    using System;
    using System.IO;
    using System.Security;
    using System.Text.RegularExpressions;
    using System.Xml;

    internal class RenameFolderCommandHandler : XmlCommandHandlerBase
    {
        protected override void BuildXml()
        {
            if (!base.CurrentFolder.CheckAcl(AccessControlRules.FolderRename))
            {
                ConnectorException.Throw(0x67);
            }
            else if (base.CurrentFolder.ClientPath == "/")
            {
                ConnectorException.Throw(0x6d);
            }
            else
            {
                string fileName = base.Request["NewFolderName"];
                if (!CKFinder.Connector.Connector.CheckFileName(fileName) || Config.Current.CheckIsHiddenFolder(fileName))
                {
                    ConnectorException.Throw(0x66);
                }
                else
                {
                    DirectoryInfo info = new DirectoryInfo(base.CurrentFolder.ServerPath);
                    bool flag = false;
                    try
                    {
                        if (!info.Exists)
                        {
                            ConnectorException.Throw(0x6d);
                        }
                        else
                        {
                            string path = Path.Combine(info.Parent.FullName, fileName);
                            if (Directory.Exists(path) || File.Exists(path))
                            {
                                ConnectorException.Throw(0x73);
                            }
                            info.MoveTo(path);
                            flag = true;
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        ConnectorException.Throw(0x68);
                    }
                    catch (SecurityException)
                    {
                        ConnectorException.Throw(0x68);
                    }
                    catch (ArgumentException)
                    {
                        ConnectorException.Throw(0x66);
                    }
                    catch (NotSupportedException)
                    {
                        ConnectorException.Throw(0x66);
                    }
                    catch (PathTooLongException)
                    {
                        ConnectorException.Throw(0x66);
                    }
                    catch (IOException)
                    {
                        ConnectorException.Throw(110);
                    }
                    catch (ConnectorException exception)
                    {
                        throw exception;
                    }
                    catch (Exception)
                    {
                        ConnectorException.Throw(110);
                    }
                    if (flag)
                    {
                        try
                        {
                            DirectoryInfo info2 = new DirectoryInfo(base.CurrentFolder.ThumbsServerPath);
                            string str3 = Path.Combine(info2.Parent.FullName, fileName);
                            if (Directory.Exists(str3))
                            {
                                File.Delete(base.CurrentFolder.ThumbsServerPath);
                            }
                            else
                            {
                                try
                                {
                                    info2.MoveTo(str3);
                                }
                                catch
                                {
                                    File.Delete(base.CurrentFolder.ThumbsServerPath);
                                }
                            }
                        }
                        catch
                        {
                        }
                        string attributeValue = Regex.Replace(base.CurrentFolder.ClientPath, "[^/]+/?$", fileName) + "/";
                        string str5 = base.CurrentFolder.ResourceTypeInfo.Url + attributeValue.TrimStart(new char[] { '/' });
                        XmlNode node = XmlUtil.AppendElement(base.ConnectorNode, "RenamedFolder");
                        XmlUtil.SetAttribute(node, "newName", fileName);
                        XmlUtil.SetAttribute(node, "newPath", attributeValue);
                        XmlUtil.SetAttribute(node, "newUrl", str5);
                    }
                }
            }
        }
    }
}

